﻿using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entitys;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

namespace RentHouse.Service
{
    public class PermissionsService : IPermissionsService
    {
        DBModel _dbModel;

        public PermissionsService(DBModel _dbModel)
        {

            this._dbModel = _dbModel;
        }

        public string Create(PermissionsDTO permissionsDTO)
        {
            T_Permissions t_Permissions = new T_Permissions()
            {
                Description = permissionsDTO.icon ?? "权限名",
                Name = permissionsDTO.title ?? "字段",
                CreateDateTime = DateTime.Now,
                IsDeleted = true
            };

            _dbModel.T_Permissions.AddOrUpdate(t_Permissions);
            int i = _dbModel.SaveChanges();

            if (i > 0)
            {
                return "succeed";
            }
            else
            {
                return "error";
            }

        }

        public string Delete(PermissionsDTO permissionsDTO)
        {
            T_Permissions t_Permissions = _dbModel.T_Permissions.Where(t => t.Id == permissionsDTO.id).FirstOrDefault();

            _dbModel.Entry(t_Permissions).State = System.Data.Entity.EntityState.Deleted;
            _dbModel.T_Permissions.Remove(t_Permissions);
            int i = _dbModel.SaveChanges();

            if (i > 0)
            {
                return "succeed";
            }
            else
            {
                return "error";
            }
        }

        public List<PermissionsDTO> GetList()
        {

            return _dbModel.T_Permissions.Select(t => new PermissionsDTO
            {
                id = (int)t.Id,
                title = t.Name ?? "权限名",
                icon = t.Description ?? "字段"
            }).ToList();

        }


        public List<PermissionsDTO> GetListAdminUserById(int Id)
        {
            //Id,Name,Description,type,href,fid,CreateDateTime
            long id = _dbModel.T_AdminUsers.Where(a => a.Name == Id.ToString()).FirstOrDefault().Id;
            string strSQL = $"select * from T_Permissions p where p.Id in (select rp.PermissionId from T_RolePermissions rp  where RoleId in (select ur.RoleId from T_AdminUserRoles ur where ur.AdminUserid = {id}))and p.IsDeleted = 1";
            List<PermissionsDTO> list = new List<PermissionsDTO>();
            var info = _dbModel.Database.SqlQuery<T_Permissions>(strSQL);
            foreach (var t in info)
            {
                PermissionsDTO dto = new PermissionsDTO()
                {
                    id = t.Id,
                    title = t.Name,
                    icon = t.Description,
                    type = t.type,
                    href = t.href,
                    fid = t.fid
                };
                list.Add(dto);
            }


            //_dbModel.T_Permissions.Select(t => new PermissionsDTO
            //{
            //    id = (int)t.Id,
            //    title = t.Name ?? "权限名",
            //    icon = t.Description ?? "字段"
            //}).ToList();

            //var list = (
            //    from a  in _dbModel.T_AdminUsers.Where(a => a.Id == Id)
            //    join ur in _dbModel.T_AdminUserRoles on a.Id equals ur.AdminUserId
            //    join r  in _dbModel.T_Roles  on ur.RoleId equals r.Id
            //    join rp in _dbModel.T_RolePermissions on ur.RoleId equals rp.RoleId
            //    join p  in _dbModel.T_Permissions.Where(p => p.IsDeleted == true) on rp.PermissionId equals p.Id
            //    select new
            //    {
            //        p.Id,
            //        p.Name,
            //        p.Description,
            //        p.type,
            //        p.href,
            //        p.fid
            //    }).Select(t => new PermissionsDTO
            //    {
            //        id = t.Id,
            //        title = t.Name,
            //        icon = t.Description,
            //        type = t.type,
            //        href = t.href,
            //        fid = t.fid
            //    }).ToList();



            //.Select(t => new PermissionsDTO
            //{
            //    id = t.Id,
            //    title = t.Name,
            //    icon = t.Description,
            //    type = t.type,
            //    href = t.href,
            //    fid = t.fid
            //}).ToList();

            return list;
        }
     

        /// <summary>
        /// 界面树结构
        /// </summary>
        /// <returns></returns>
        public List<PermissionsDTO> GetPermis()
        {

            List<PermissionsDTO> list = _dbModel.T_Permissions.Where(t => t.IsDeleted == true).Select(t => new PermissionsDTO
            {
                id = (int)t.Id,
                title = t.Name,
                icon = t.Description,
                type = t.type,
                href = t.href,
                fid = t.fid
            }).ToList();
            return list;
        }


        public string Update(PermissionsDTO permissionsDTO)
        {
            T_Permissions t_Permissions = _dbModel.T_Permissions.Where(t => t.Id == permissionsDTO.id).FirstOrDefault();

            t_Permissions.Name = permissionsDTO.title ?? t_Permissions.Name;
            t_Permissions.Description = permissionsDTO.icon ?? t_Permissions.Description;

            _dbModel.T_Permissions.AddOrUpdate(t_Permissions);
            int i = _dbModel.SaveChanges();

            if (i <= 0)
            {
                return "error";
            }
            else
            {
                return "succeed";
            }
        }
    }
}
